package com.crm.service.saling.impl;

import com.crm.dao.client.ClientInfoDao;
import com.crm.dao.client.ContactDao;
import com.crm.dao.saling.ContractDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.Contact;
import com.crm.pojo.Contract;
import com.crm.pojo.Inandoutlog;
import com.crm.service.saling.ContractService;
import com.crm.vo.ContractVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/3
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ContractServiceImpl implements ContractService {
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ClientInfoDao clientInfoDao;
    @Autowired
    private ContactDao contactDao;

    @Override
    public Contract saveContract(Contract contract) {
        return contractDao.save(contract);
    }

    @Override
    public Contract getOneContract(Long contractId) throws ServiceException {

        try {
            return contractDao.findOne(contractId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 显示合同数量
     *
     * @return
     */
    @Override
    public Long getContractExistCount() throws ServiceException {
        try {
            return contractDao.getContractExistCount();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 分页显示合同
     *
     * @param currpage 当前页
     * @param rows     页大小
     * @return
     */
    public List<ContractVO> showContractAllList(int currpage, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(currpage - 1, rows);
            List<ContractVO> list = contractDao.queryContractListAllByPage(pageRequest);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /**
     * 通过id查询联系人
     *
     * @param clienId
     * @return
     */
    @Override
    public Contact findContactById(Long clienId) {
        return contactDao.findOne(clienId);
    }

    @Override
    public List<ContractVO> showContractList() {
        return contractDao.showContractList();
    }


    /**
     * 删除合同
     *
     * @param contractId
     * @return
     */
    @Override
    public Result deleteContractById(Long contractId) throws ServiceException {

        Result s = null;
        try {
            contractDao.deleteContractById(contractId);
            s = Result.SUCCESS;
        } catch (Exception e) {
            s = new Result(e);
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return s;
    }


    /**
     * 加载单个合同数据
     *
     * @param ContractId
     * @return
     */
    @Override
    public ContractVO getLoadContractVO(Long ContractId) throws ServiceException {
        try {
            return contractDao.getLoadContractVO(ContractId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }

    }


    //模糊查询订单标题或订单id
    public List<Inandoutlog> findAllOrderBymh(String q) {
        List<Inandoutlog> l = new ArrayList<>();
        List<Contract> clist = contractDao.getAllOrderBymh("%" + q + "%");
        for (Contract c : clist) {
            Inandoutlog io = new Inandoutlog();
            io.setIoDispatherTitle(c.getContractTopic());
            io.setIoDispatherId(c.getContractId());
            l.add(io);
        }
        return l;
    }

    //查询单个订单合同通过id
    public Contract findOneById(Long ContractId) {
        return contractDao.findOne(ContractId);
    }


}
